<?php
	/*
		PandaForum: PHP Forum Engine
		
		Jesse Gill, 0108377707
	*/
	
	require_once('../classes/DBConnection.php');
	require('../config.php');
	
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	
	$connection = new DBConnection();
	$connection->connect($host, $username, $password, $dbname);
	
	if (! $connection->is_connected())
	{
		die($connection->get_error());
	}
	
	$id = 					$connection->clean_var($_GET['id']);
	$page = 				$connection->clean_var(intval($_GET['page']));
	
	$result = $connection->get_result("SELECT * FROM post WHERE topicid=$id");
	
	if (! $result)
		die($connection->get_error());
	
	$numpages = ceil($result->num_rows / $postsperpage);
	
	$lowerlimit = ($page - 1) * $postsperpage;
	$upperlimit = $postsperpage;
	
	$result->free();
	
	$result = $connection->get_result("SELECT * FROM post WHERE topicid=$id ORDER BY creationdate ASC LIMIT $lowerlimit, $upperlimit");
	
	if (! $result)
		die($connection->get_error());
	
	$i = 1;
?>
	<table cellspacing="0">
<?php
	while ($obj = $result->fetch_object())
	{
		$posterinfo = $connection->get_result_array("SELECT username FROM user WHERE id=$obj->posterid");
		$postername = (! empty($posterinfo)) ? $posterinfo['username'] : "-";
?>
		<tr class="postrow-<?php echo $i; ?>">
			<td class="posterinfo">
				<?php echo $postername; ?>
			</td>
			
			<td class="postbody">
				<?php echo prepare_post($obj->body); ?>
			</td>
		</tr>
<?php
		if ($i == 1)
		{
			$i = 2;
		}
		else
		{
			$i = 1;
		}
	}
	
?>
	</table>
	
	<div id="pagelist">
<?php
		for ($p = 0; $p < $numpages; $p++)
		{
?>
			<a href="javascript:viewtopic_posts_refreshlist(<?php echo $id; ?>, <?php echo ($p + 1); ?>)"><?php echo ($p + 1); ?>, </a>
<?php
		}
?>
	</div>
<?php
	$result->free();
	$connection->close();
	
	function prepare_post($body)
	{
		$cleanvar = filter_var($body, FILTER_SANITIZE_STRING);
		
		$cleanvar = stripslashes($cleanvar);
		
		$arr = array("\n" => "<br />");
		
		$cleanvar = strtr($cleanvar, $arr);
		
		return $cleanvar;
	}
?>
